
	$(function(){
		//创建3个不同上传文件的不同url Map
		var fileUrl = {
				"songFile": window.url.UPLOAD_MP3,
				"imgFile" : window.url.UPLOAD_IMG,
				"lyrFile" : window.url.UPLOAD_LYR
				};
		var fileUploadStatus = {};
		
		$("#songFile").fileinput({
	        showUpload : false,
	        showRemove : false,
	        showPreview: false,
	        language : 'zh',
	        dropZoneEnabled: false,
	        allowedPreviewTypes: ['audio'],
	        allowedFileTypes: ['audio'],
	        allowedFileExtensions:  ['mp3'],
	        maxFileSize : 15360,
	    }).on("filebatchselected", function(event, files) {
	    	hideErrorMsg(this);
	    	var file = $(this)[0].files[0];
	    	var fileName = file.name;
	    	//根据歌曲文件名,截取歌曲名和歌手显示到文本宽
	    	fillInput(fileName);
	    });
		$("#lyrFile").fileinput({
	        showUpload : false,
	        showRemove : false,
	        showPreview: false,
	        language : 'zh',
	        dropZoneEnabled: false,
	        maxFileSize : 100,
	    }).on("filebatchselected", function(event, files) {
	    	hideErrorMsg(this);
	    });
		
		$("#imgFile").fileinput({
	        showUpload : false,
	        showRemove : false,
	        //showPreview: false,
	        language : 'zh',
	        dropZoneEnabled: false,
	        allowedPreviewTypes: ['image'],
	        allowedFileTypes: ['image'],
	        allowedFileExtensions:  ['png','jpg','jpeg'],
	        maxFileSize : 2048,
	    }).on("filebatchselected", function(event, files) {
	    	hideErrorMsg(this);
	    });
		//重新选择文件后隐藏错误信息
		function hideErrorMsg(obj) {
			var thisName = $(obj).attr("name");
	    	$("."+thisName).text("").hide();
		}
		
		function fillInput(fileName) {
			  $.ajax({
				  type: "post",
				  url: window.url.CONVERT,
				  data: {"fileName": fileName},
				  async: false,
				  success: function(data) {
					  $("#songName").val(data.songName);
					  $("#singer").val(data.singer);
				  }
			  });
		}
		
		$("#songSubmit").click(function(){
			//alert();
			if(!$("#songform").valid())
				return false;
			var songId = $("#songId").val();
			if(songId != "") 
				songForm(true);
			else
				//上传文件
				uploadFile();
		});
		
		function uploadFile() {
			  var fd = null;
			  var name = "";
			  var status = "";
			  var url = "";
			  var files = $(":file");
			  var hasError = false;
			  $.each(files, function(index, item){
			  	fd = new FormData();
			  	name = $(item).attr("name");
			  	status = fileUploadStatus[name];
			  	//如何当前文件已上传成功,则跳过，进行下一个文件上传
			  	if(status == "true") 
			  		return true;
			  	url = fileUrl[name];
			  	fd.append(name, item.files[0]);
			  	$.ajax({
						type: 'post',
						url: url,
						data: fd,
						async: false,  
					    cache: false,  
					    contentType: false,  
					    processData: false, 
						success: function(data){
							if(data.code == 0) {
								fileUploadStatus[name] = "true";
							}
							if(data.code == 1){
								$("."+name).text(data.message).show();
								hasError = true;
							}
						}
					});
			  	//有文件上传失败,则终止循环
			  	if(hasError) {
			  		return false;
			  	}
			  });
			  //全部上传成功后才提交表单
			  if(!hasError) 
			  	songForm(false);
			}
			//服务端提交数据
			function songForm(isUpdate) {
				//json格式:{songName : '大王来巡山'，singer:'贾乃亮'}
				var param = {
					'id' : $("#songId").val(),
					'songName' : $("#songName").val(),
					'singer' : $("#singer").val(),
					'album' : $("#album").val()
				}
				if(!isUpdate) {
					
					param['fileName'] = $("#songFile").val();
					param['imageName'] = $("#imgFile").val();
					param['lyricName'] = $("#lyrFile").val();
				}
				$.post(url.NEW_SONG,param, function(data){
					if(data.code == 0){
						var successMsg = "";
						var songId = $("#songId").val();
						if(songId == "") {
							successMsg = "添加歌曲成功";
						}else{
							successMsg = "修改歌曲成功";
						}
						
						layer.msg(successMsg, {
							icon : 1,
							time : 1000
						}, function() {
							window.parent.location.reload(); // 刷新父页面
						});
					}else if(data.code == 1) {
						layer.msg(result.message, {
							icon : 2,
							time : 1000
						});
					}
				});
			};
	})
